<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller 
{
    public function __construct() 
    {	
        parent::__construct();
        $this->load->model('advertising/advertising_model');
    }
    
    function index() 
    {
        $this->load->helper('config');
        
        $advertising_arr = $this->advertising_model->get_all();
        $view_data_arr = array('advertising_arr' => $advertising_arr);
        
        $this->render($view_data_arr);
    }
    
    public function add($id = 0) 
    {
        $this->load->helper('config');
      
        $this->edit($id);
    }
    
    public function edit($id) 
    {
      //$this->advertising_model->test(); 
      $this->load->helper('config');
      $this->load->library('form_validation');

        $id = (int) $id;
        $view_data_arr = array();
        
        if ($this->form_validation->run('advertising/form') == FALSE) {
            if ($id) {
                $view_data_arr['advertising'] = $this->advertising_model->get_by_id($id);
            }
            
            $this->render($view_data_arr);
            return FALSE;            
        } else {
            $_POST['image'] = $this->form_validation->set_value('image');
            
            if ($id) {
                $this->advertising_model->update($id, $_POST);
                $this->session->set_flashdata('success_msg', 'Updated successfully!');
            } else {
                $this->advertising_model->create($_POST);
                $this->session->set_flashdata('success_msg', 'Added successfully!');
            }
        
            redirect('advertising/index/index', 'refresh');
            return TRUE;
        }
    }
    
    /**
     * Privace Callbacks function for Validation image
     * 
     * @param unknown_type $file
     * @return unknown_type
     */
	public function image_check($file)
	{
	    // Try upload file
        $upload_result_arr = $this->_do_upload('image', array(
            'upload_path' => './upload/advertising/',
            'allowed_types' => 'gif|jpg|png|jpeg',
            'max_size' => '2048',
            'max_width' => '1280',
            'max_height' => '768'
        ));
        
		if (isset($upload_result_arr['error'])) {
			$this->form_validation->set_message('image_check', $upload_result_arr['error']);
			return FALSE;
		}
        
		if ($_POST['type'] == Config_type_helper::TYPE_AD) {
		    $width = '50';
		    $height = '50';
		} else {
		    $width = '440';
		    $height = '280';
		}
		
		// try resize image upload
        $resize_result_arr = $this->_do_resize(array(
            'image_library' => 'gd2',
            'source_image' => './upload/advertising/' . $upload_result_arr['file_name'],
            'width' => $width,
            'height' => $height,
            'maintain_ratio' => FALSE
        ));
        
		if (isset($resize_result_arr['error'])) {
			$this->form_validation->set_message('image_check', $resize_result_arr['error']);
			return FALSE;
		}
  
        return $upload_result_arr['file_name'];
	}
    
	/**
	 * Upload file
	 * 
	 * @param unknown_type $file
	 * @param unknown_type $config_arr
	 * @return multitype:NULL |multitype:NULL
	 */
	private function _do_upload($file, $config_arr)
	{
        $this->load->library('upload', $config_arr);
            
		if (!$this->upload->do_upload($file)) {
            return array('error' => $this->upload->display_errors());
	    } else {
			return array('file_name' => $this->upload->file_name);
		}
	}
    
    private function _do_resize($config_arr) {
        $this->load->library('image_lib', $config_arr);

        if (!$this->image_lib->resize()) {
            return array('error' => $this->image_lib->display_errors());
        } else {
            return TRUE;
        }
    }
}